package com.woniuxy.service.impl;

import com.woniuxy.dao.LoginmsgDao;
import com.woniuxy.dao.ManagerDao;
import com.woniuxy.dao.UserDao;
import com.woniuxy.entity.Loginmsg;
import com.woniuxy.entity.Member;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author: GYF
 * Date: 2021/2/23:14:26
 * Description:登入用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserDao userDao;
    private ManagerDao managerDao;
    private LoginmsgDao loginmsgDao;


    /**
     * Description : 登入服务
     * ChangeLog : 1. 创建 (2021/2/23 14:56 GYF);
     * @param user
     * @return int
     **/
    @Override
    public int login(User user, HttpSession session) {
        int re=0;
        //获取数据库密码
        System.out.println(user.getAccount());
        User realUser = userDao.selectByAccount(user.getAccount());
        System.out.println(realUser);
        //账号不存在
        if (realUser==null){
            re=-1;
        }else {
            //比较密码
            if (realUser.getPassword().equals(user.getPassword())) {
                //登入成功
                //查询该用户的member信息
                Member member=managerDao.selectOne(realUser.getMemberid());
                System.out.println(member);
                //存入session
                session.setAttribute("account",realUser);
                session.setAttribute("member",member);
                String name = managerDao.selectByAccount(user.getAccount());
                Loginmsg loginmsg = new Loginmsg();
                loginmsg.setName(user.getAccount());
                loginmsg.setRealname(name);
                loginmsg.setTel(user.getAccount());
                loginmsgDao.insert(loginmsg);
                re = 1;
            } else {
                //登入失败
            }
        }
        return re;
    }

    /**
     * Description : 添加管理员账号
     * ChangeLog : 1. 创建 (2021/2/24 14:22 GYF);
     * @param user
     * @return int
     **/
    @Override
    public int registerUser(User user) {
        int i = userDao.registerUser(user);
        //存入默认权限
        int[] roles = {2,3};
        int ii = userDao.addRoles(user.getId(), roles);
        return ii;
    }

    /**
     * Description : 查询所有后台用户
     * ChangeLog : 1. 创建 (2021/2/25 14:46 GYF);
     *
     * @return java.util.List<com.woniuxy.entity.User>
     **/
    @Override
    public List<User> queryAdmin() {
        List<User> users = userDao.selectAllByMemberid();
        return users;
    }

    /**
     * Description : 更新用户权限
     * ChangeLog : 1. 创建 (2021/2/25 14:46 GYF);
     * @param id
     * @param roles 权限id数组
     * @return int
     **/
    @Override
    public int changeRole(int id, int[] roles) {
        //先删除当前权限

        //再添加新权限
        int i = userDao.addRoles(id, roles);
        return i;
    }
}
